import { decrypt } from '@/utils/jsencrypt'


//参数container为图表盒子节点.charts为图表节点
export function chartssize(container, charts) {
  function getStyle(el, name) {
    if (window.getComputedStyle) {
      return window.getComputedStyle(el, null);
    } else {
      return el.currentStyle;
    }
  }
  var wi = getStyle(container, 'width').width;
  var hi = getStyle(container, 'height').height;
  charts.style.width = wi
  charts.style.height = hi
}

export function dataProcess(data) {
  let temp = data.map((item, index) => {
    return {
      name: item.name,
      value: parseInt(item.value),
      url: item.url,
      params: getUrlkey(item.url)
    }
  })
  return temp;
}

export function dataProcessLabel(data) {
  let temp = data.map((item, index) => {
    return {
      name: item.name,
      value: parseInt(item.value),
      url: item.url,
      label: { show: parseInt(item.value) ? true : false },
      params: getUrlkey(item.url)
    }
  })
  return temp;
}

export function dataSum(data) {
  var sum = 0;
  for (var i = data.length - 1; i >= 0; i--) {
    sum += parseInt(data[i].value);
  }
  return sum;
}

export const setInfoDataMixins = {
  data() { },
  created() { },
  methods: {
    setInfoData(e) {
      var idcard = e.target.value;
      this.$refs['form'].validateField('ctIdcard', (valid) => {
        if (!valid) {
          var province = idcard.substring(0, 6);
          var birthday = idcard.substring(6, 10) + '-' + idcard.substring(10, 12) + '-' + idcard.substring(12, 14);
          var sex = parseInt(idcard.substr(16, 1)) % 2 == 1 ? "SEX1" : "SEX2";
          //this.$set(this.form, 'ctBirthplace', 'JG'+province);
          this.$set(this.form, 'ctBirthplace', province);
          this.$set(this.form, 'dtBirthday', birthday);
          this.$set(this.form, 'ctSex', sex);
        }
      })
    }
  }
}

function getUrlkey(url) {
  var params = {};
  if (url && url.indexOf("?") != -1) {
    var urls = url.split("?");

    var arr = urls[1].split("&");
    for (var i = 0, l = arr.length; i < l; i++) {
      var a = arr[i].split("=");
      params[a[0]] = a[1];
    }

  }
  return params;
}

/**
 * 获取funNo
 * @param {路由} 传入this.$route
 * @returns funNo
 */
function getFunNo(route) {
  const regex = /^\d{4}$/
  if (route.name) {
    const funNo = route.name.slice(-4)
    if (regex.test(funNo)) {
      return funNo
    }
  }
  if (regex.test(route.query.funNo)) {
    return route.query.funNo
  }
  if (regex.test(route.query.ctFunNo)) {
    return route.query.ctFunNo
  }
}

export { getUrlkey, getFunNo }

